// pages/component/search/search.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  ready: function () {
    let that = this

    wx.request({
      url: app.globalData.url + 'ny_flow.php?action=get_category',
      success: function (res) {
        var pages = getCurrentPages()    //获取加载的页面
        var currentPage = pages[pages.length - 1]    //获取当前页面的对象
        var url = currentPage.route   
        if(url != 'pages/wholesale/wholesale'){
          let arr = []
          res.data.map((item) => {
            arr.push(item.cat_name)
          })
          that.setData({
            array: arr
          })
        }else{
          let arr = []
          res.data.map((item) => {
            arr.push(item.cat_name)
          })
          arr.reverse();
          that.setData({
            array: arr
          })
        }
      },
    })

  },
  /**
   * 组件的初始数据
   */
  data: {
    array: [0, 1, 2],
    index: 0,
    content: '',
    pro: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindPickerChange: function (e) {
      this.setData({
        index: e.detail.value
      })
    },
    keywords: function (e) {
      this.setData({
        content: e.detail.value
      })
    },
    sea: function () {
      var that = this;
      var pages = getCurrentPages()    //获取加载的页面
      var currentPage = pages[pages.length - 1]    //获取当前页面的对象
      var url = currentPage.route    //当前页面url
      if (url === "pages/index/index") {
        switch (Number(this.data.index)) {
          case 0:
            wx.navigateTo({
              url: '../market/market?val=' + this.data.content
            })
            break
          case 1:
            wx.navigateTo({
              url: '../wholesale/wholesale?val=' + this.data.content
            })
            break
        }
      } else {
        wx.request({
          url: app.globalData.url + 'ny_flow.php?action=search',
          data: {
            cat_id: this.data.index,
            keywords: this.data.content
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            if(res.data.length == 0){
              wx.showToast({
                title: '抱歉，没有搜索结果',
                icon:'none'
              })
              var myEventDetail = {
                val: res
              } // detail对象，提供给事件监听函数
              that.triggerEvent('myevent', myEventDetail) //myevent自定义名称事件，父组件中使用
            }else{
              wx.showToast({
                title: '搜索结果显示成功',
                icon: 'none'
              })
              var myEventDetail = {
                val: res
              } // detail对象，提供给事件监听函数
              that.triggerEvent('myevent', myEventDetail) //myevent自定义名称事件，父组件中使用
            }
          }
        })
      }
    }
  }
})


